package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ly.mapper.UserMapper;
import com.ly.pojo.EduConstant;
import com.ly.pojo.User;
import com.ly.pojo.UserDTO;
import com.ly.service.UserService;
import com.ly.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    @Qualifier("diyRedisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private UserMapper userMapper;
    /**
     * 登录验证并携带token
     *
     * @param phone
     * @param password
     * @return
     */
    @Override
    public UserDTO<User> login(String phone, String password) {
        UserDTO<User> dto = new UserDTO<>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            //通过手机号查询为空
            dto.setMessage(EduConstant.ERROR_NOT_FOUND_PHONE);
            dto.setState(EduConstant.ERROR_NOT_FOUND_PHONE_CODE);
            dto.setContent(null);
            return dto;
        } else {
            //查询到此手机号时
            wrapper.eq("password", password);
            User one = userMapper.selectOne(wrapper);
            if (one == null) {
                dto.setState(EduConstant.ERROR_PASSWORD_CODE);
                dto.setMessage(EduConstant.ERROR_PASSWORD);
                dto.setContent(null);
                return dto;
            } else {
                //登录成功！
                dto.setState(EduConstant.LOGIN_SUCCESS_CODE);
                dto.setMessage(EduConstant.LOGIN_SUCCESS);
                dto.setContent(one);
                String token = JwtUtil.createToken(one);
                redisTemplate.opsForValue().set("token", token, 60*15, TimeUnit.SECONDS);
                dto.setToken(token);
                return dto;
            }
        }

    }

    /**
     * 验证token
     *
     * @param token
     * @return
     */
    @Override
    public UserDTO<User> check(String token) {
        UserDTO<User> dto = new UserDTO<User>();
        //从前台传来的token和redis中的token验证
        String oldToken = (String) redisTemplate.opsForValue().get("token");
        if (oldToken.equals(token)){
            int i = JwtUtil.isVerify(oldToken);
            if (i == 0) {
                dto.setState(EduConstant.TOKEN_SUCCESS_CODE);
                dto.setMessage(EduConstant.TOKEN_SUCCESS);
                // 校验通过，重新设置过期时间
                redisTemplate.opsForValue().set("token", oldToken, 60*15, TimeUnit.SECONDS);
            } else if (i == 1) {
                dto.setState(EduConstant.TOKEN_TIMEOUT_CDOE);
                dto.setMessage(EduConstant.TOKEN_TIMEOUT);
            } else if (i == 2) {
                dto.setState(EduConstant.TOKEN_NULL_CODE);
                dto.setMessage(EduConstant.TOKEN_ERROR1);
            } else {
                dto.setState(EduConstant.TOKEN_ERROR_CDOE);
                dto.setMessage(EduConstant.TOKEN_ERROR2);
            }
            return dto;
        }
        return null;

    }
}
